package com.yunsom.testdemo.collection;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;

/**
 * @author Xbao
 * @version 1.0
 * @description
 * @date 2021/3/3 19:48
 */
public class Maptest {

  private static Map<MyHashMap, Integer> integerMap = new HashMap<>();
  private static CountDownLatch countDownLatch = new CountDownLatch(10);

  public static void main(String[] args) {

    Map<String,Integer> map = new HashMap<>();
    map.forEach((k,v) -> {
      System.out.println(k);
      System.out.println(v);
    });

    Map<Integer, Byte> hourTimeRangeMapByMonth = new HashMap<>();
    hourTimeRangeMapByMonth.put(12, (byte) 2);
    hourTimeRangeMapByMonth.put(4, (byte) 4);
    hourTimeRangeMapByMonth.put(1, (byte) 2);
    hourTimeRangeMapByMonth.put(10, (byte) 3);
    hourTimeRangeMapByMonth.put(2, (byte) 2);
    hourTimeRangeMapByMonth.put(3, (byte) 1);
    final List<Integer> sorted = hourTimeRangeMapByMonth.keySet().stream().sorted().collect(
        Collectors.toList());
    for (Integer integer : sorted) {
      System.out.println(integer);
    }

    // 测试map填充的性能
//    Map<Integer, Byte> hourWithTypeMap = new HashMap<>();
//    System.out.println(hourWithTypeMap.get(1));
//
//    final MyHashMap myHashMap1 = new MyHashMap();
//    final MyHashMap myHashMap2 = new MyHashMap();
//    final MyHashMap myHashMap3 = new MyHashMap();
//    final MyHashMap myHashMap4 = new MyHashMap();
//    final MyHashMap myHashMap5 = new MyHashMap();
//    final MyHashMap myHashMap6 = new MyHashMap();
//    final MyHashMap myHashMap7 = new MyHashMap();
//    final MyHashMap myHashMap8 = new MyHashMap();
//    final MyHashMap myHashMap9 = new MyHashMap();
//    final MyHashMap myHashMap10 = new MyHashMap();
//
//    long startTime = System.currentTimeMillis();
//    myHashMap1.run();
//    myHashMap2.run();
//    myHashMap3.run();
//    myHashMap4.run();
//    myHashMap5.run();
//    myHashMap6.run();
//    myHashMap7.run();
//    myHashMap8.run();
//    myHashMap9.run();
//    myHashMap10.run();
//    try {
//      countDownLatch.await();
//      long endTime = System.currentTimeMillis();
//      System.out.println("用时：" + (endTime - startTime)/1000 + "s");
//    } catch (Exception e){
//      e.printStackTrace();
//    }

//    Map<String, Soldier> map = new HashMap<>(4);
//    map.put("1",new Soldier("士兵1","手枪"));
//    map.put("2",new Soldier("士兵2","手枪"));
//    map.put("3",new Soldier("士兵3","手枪"));
//    map.put("4",new Soldier("士兵4","手枪"));
//
//    map.forEach((key, value) -> {
//      System.out.println("key: " + key);
//      System.out.println("value: " + value);
//    });
//
////    MultiValueMap
//    ConcurrentHashMap<Integer, Soldier> soldierConcurrentHashMap = new ConcurrentHashMap<>(10);
//    soldierConcurrentHashMap.put(1, new Soldier("1", "1"));
//    soldierConcurrentHashMap.put(1, new Soldier());
//    soldierConcurrentHashMap.put(1, null);
//    System.out.println("---end---");

  }

  static class MyHashMap implements Runnable{

    @Override
    public void run() {
      Random random = new Random();

      try{
        for (int i = 0; i < 10000; i++) {
          if(integerMap.containsKey(this)){
            integerMap.put(this, integerMap.get(this) + random.nextInt(10));
          } else {
            integerMap.put(this, random.nextInt(10));
          }
        }
      } catch (Exception e){
        e.printStackTrace();
      } finally {
        countDownLatch.countDown();
      }
    }
  }
}
